unit quicklinks;
(*  cssMonkey
    Copyright (C) 2011 Matthew Hipkin <http://www.matthewhipkin.co.uk>

    This Source Code Form is subject to the terms of the Mozilla Public
    License, v. 2.0. If a copy of the MPL was not distributed with this
    file, You can obtain one at http://mozilla.org/MPL/2.0/. *)
{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  Menus, Buttons, ComCtrls, miscfunc;

type

  { TfrmManageQL }

  TfrmManageQL = class(TForm)
    btnDone: TButton;
    btnNew: TButton;
    btnUpdate: TButton;
    btnDelete: TButton;
    GroupBox1: TGroupBox;
    labelPreview: TLabel;
    listItems: TListBox;
    menuDelete: TMenuItem;
    menuEdit: TMenuItem;
    menuNew: TMenuItem;
    menuLinks: TPopupMenu;
    procedure btnDeleteClick(Sender: TObject);
    procedure btnDoneClick(Sender: TObject);
    procedure btnNewClick(Sender: TObject);
    procedure btnUpdateClick(Sender: TObject);
    procedure listItemsClick(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  frmManageQL: TfrmManageQL;

implementation

uses elements;

{$R *.lfm}

{ TfrmManageQL }

procedure TfrmManageQL.btnUpdateClick(Sender: TObject);
var
  f: ^TFavItem;
  l: TListItem;
  i: integer;
  v: string;
  tmp: String;
begin
  if listItems.ItemIndex < 0 then exit;
  f := qLinks[listItems.ItemIndex];
  frmElement.Caption := 'Editing '+f^.key;
  frmElement.tags.LoadFromFile(appdir+'taglist.txt');
  frmElement.elements.LoadFromFile(appdir+'elementlist.txt');
  frmElement.textAttributes.Text := '';
  frmElement.textElementName.Text := f^.key;
  frmElement.textValues.Text := '';
  frmElement.checkShow.Visible := true;
  frmElement.listAttributes.Items.Clear;
  frmElement.checkShow.Checked := f^.showButton;
  for i := 0 to High(f^.values) do
  begin
    l := frmElement.listAttributes.Items.Add;
    tmp := f^.values[i];
    l.Caption:=Copy(tmp,1,Pos(':',tmp)-1);
    l.SubItems.Add(Copy(tmp,Pos(':',tmp)+1,Length(tmp)));
  end;
  if frmElement.ShowModal = mrOK then
  begin
    f^.key := frmElement.textElementName.Text;
    f^.values := nil;
    v := '';
    for i := 0 to frmElement.listAttributes.Items.Count-1 do
    begin
      v := v + frmElement.listAttributes.Items[i].Caption + ':' + frmElement.listAttributes.Items[i].SubItems[0] + ';';
    end;
    f^.values := explode(';',v,0);
    listItems.Items[listItems.ItemIndex] := f^.key;
    f^.showButton := frmElement.checkShow.Checked;
  end;
end;

procedure TfrmManageQL.btnDoneClick(Sender: TObject);
begin
  ModalResult := mrOK;
end;

procedure TfrmManageQL.btnNewClick(Sender: TObject);
var
  f: ^TFavItem;
  i: integer;
  v: String;
begin
  frmElement.Caption:='New Element';
  frmElement.tags.LoadFromFile(appdir+'taglist.txt');
  frmElement.elements.LoadFromFile(appdir+'elementlist.txt');
  frmElement.textAttributes.Text := '';
  frmElement.textElementName.Text := '';
  frmElement.textValues.Text := '';
  frmElement.listAttributes.Items.Clear;
  frmElement.checkShow.Visible := true;
  frmElement.checkShow.Checked := true;
  if frmElement.ShowModal = mrOK then
  begin
    new(f);
    f^.key := frmElement.textElementName.Text;
    f^.showButton := frmElement.checkShow.Checked;
    v := '';
    for i := 0 to frmElement.listAttributes.Items.Count-1 do
    begin
      v := v + frmElement.listAttributes.Items[i].Caption + ':' + frmElement.listAttributes.Items[i].SubItems[0] + ';';
    end;
    f^.values := explode(';',v,0);
    qLinks.Add(f);
    listItems.Items.Add(f^.key);
  end;
end;

procedure TfrmManageQL.btnDeleteClick(Sender: TObject);
var
  f: ^TFavItem;
begin
  if listItems.ItemIndex < 0 then exit;
  f := qLinks[listItems.ItemIndex];
  qLinks.Delete(listItems.ItemIndex);
  dispose(f);
  listItems.Items.Delete(listItems.ItemIndex);
end;

procedure TfrmManageQL.listItemsClick(Sender: TObject);
var
  f: ^TFavItem;
  x: integer;
begin
  if listItems.ItemIndex < 0 then exit;
  f := qLinks[listItems.ItemIndex];
  labelPreview.Caption := f^.key + '{' + #13#10;
  for x := 0 to High(f^.values) do
    labelPreview.Caption := labelPreview.Caption + '  ' + f^.values[x] + ';' + #13#10;
  labelPreview.Caption := labelPreview.Caption + '}';
end;

end.

